package com.liuwei.spring.cloud.junit.leetcode.sorts;

import com.liuwei.spring.cloud.common.utils.Jackson;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class QuickSort {

    public static void main(String[] args) {
        List<Integer> values = Arrays.asList(10, 5, 68,39, 17,68,null);
        List<Integer> sorted = values.stream().filter(x->x!=null).distinct().sorted().collect(Collectors.toList());
        log.info("list:{}", Jackson.toJSONString(sorted));
        log.info("minN:{}", minN(sorted,2));
        log.info("maxN:{}", maxN(sorted,2));
    }

    public static int minN(List<Integer> sorted,int n){
        if(n<0){
            return -1;
        }
        if(n==0){
            return 0;
        }
        if(n>sorted.size()){
            return -1;
        }
        List<Integer> sub = sorted.subList(0,n);
        return sum(sub);
    }

    public static int maxN(List<Integer> sorted,int n){
        if(n<0){
            return -1;
        }
        if(n==0){
            return 0;
        }
        if(n>sorted.size()){
            return -1;
        }
        List<Integer> sub = sorted.subList(sorted.size()-n,sorted.size());
        return sum(sub);
    }

    public static int sum(List<Integer> sorted){
        if(sorted==null){
            return 0;
        }
        int sum = 0;
        for(int i=0;i<sorted.size();i++){
            sum = sum + sorted.get(i);
        }
        return sum;
    }
}
